package com.zscat.mallplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.membervail.entity.MemberVail;
import com.zscat.mallplus.service.MemberVailService;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 实名认证提交
 * @author king.wyx@qq.com
 * @date 2020-09-07
 */
@Slf4j
@RestController
@RequestMapping("/api/memberVail")
public class MemberVailController {
    @Autowired
    private IUmsMemberService memberService;
    @Autowired
    private MemberVailService memberVailService;

    /**
     * 实名认证接口回显
     *
     * @return
     */
    @GetMapping("/view")
    public CommonResult view() {
        Long memberId = memberService.getNewCurrentMember().getId();
        MemberVail memberVail = memberVailService.getOne(new QueryWrapper<MemberVail>().lambda().eq(MemberVail::getUserId, memberId));
        return new CommonResult().success(memberVail);
    }


    @PostMapping("/add")
    private CommonResult add(@RequestBody MemberVail memberVail) {
        Long memberId = memberService.getNewCurrentMember().getId();
        memberVail.setUserId(memberId);
        if (memberVail.getId() == null) {
            memberVail.setStatus(1);
            memberVail.setCreatTime(new Date());
        } else {
            MemberVail memberVail1 = memberVailService.getById(memberVail.getId());
            if (memberVail1.getStatus() == 2) {
                return new CommonResult().failed("审核通过不可修改");
            }
        }

        memberVailService.saveOrUpdate(memberVail);
        return new CommonResult().success(null);
    }
}
